package com.example.bottom.view;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.example.bottom.R;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.bottomnavigation.LabelVisibilityMode;

import androidx.appcompat.app.AppCompatActivity;

public class BottomNavigation1Activity extends AppCompatActivity {

    private BottomNavigationView bottomNavigationView;
    private BottomNavigationView bottom;
    private int i = 0;
    private int k = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bottom_navigation1);
        bottomNavigationView = findViewById(R.id.nav_view);
        bottom = findViewById(R.id.nav_view1);
        Menu menu = bottom.getMenu();
        menu.add(0, 0, 0, "0");
        menu.add(0, 1, 0, "1");
        menu.add(0, 2, 0, "2");
        menu.add(0, 3, 0, "3");
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt1:
                Menu menu = bottomNavigationView.getMenu();
                if (menu.size() == 5) {
                    Toast.makeText(this, "不能在加了", Toast.LENGTH_SHORT).show();
                    return;
                }

                int id = (int) (Math.random() * 10);
                menu.add(0, id, 0, "加" + id);
                MenuItem item = menu.findItem(id);
                item.setIcon(R.drawable.main_unselected);
                break;
            case R.id.bt2:
                Menu menu1 = bottomNavigationView.getMenu();
                if (menu1.size() == 2) {
                    Toast.makeText(this, "不能在减了", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (menu1.findItem(0) != null)
                    menu1.removeItem(0);
                else
                    menu1.removeItem(bottomNavigationView.getMenu().getItem(0).getItemId());
                break;
            case R.id.bt3:
                if (i == 0)//3个或更少是LABEL_VISIBILITY_LABELED，4个或更多是LABEL_VISIBILITY_SELECTED，不能超过5个
                    bottomNavigationView.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_AUTO);
                else if (i == 1)//只有选中的才显示icon和文字
                    bottomNavigationView.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_SELECTED);
                else if (i == 2)//一直显示
                    bottomNavigationView.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED);
                else if (i == 3)//只显示icon
                    bottomNavigationView.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_UNLABELED);
                i++;
                if (i > 3) i = 0;
                break;
            case R.id.bt4:
                if (k == 0)
                    bottom.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_AUTO);
                else if (k == 1)
                    bottom.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_SELECTED);
                else if (k == 2)
                    bottom.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED);
                else if (k == 3)
                    bottom.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_UNLABELED);
                k++;
                if (k > 3) k = 0;
                break;
        }
    }
}